package xyz.zhzby.cyzx.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import xyz.zhzby.cyzx.com.convert.DiscussionConvert;
import xyz.zhzby.cyzx.com.dto.request.DiscussionAddRequest;
import xyz.zhzby.cyzx.com.dto.request.DiscussionPageRequest;
import xyz.zhzby.cyzx.com.dto.request.DiscussionReportRequest;
import xyz.zhzby.cyzx.com.dto.response.DiscussionResponse;
import xyz.zhzby.cyzx.com.entity.Discussion;
import xyz.zhzby.cyzx.com.mapper.DiscussionMapper;
import xyz.zhzby.cyzx.com.service.IDiscussionService;
import xyz.zhzby.cyzx.security.entity.User;
import xyz.zhzby.cyzx.security.util.CyzxSecurityContextHolder;
import xyz.zhzby.cyzx.servicebase.domain.DataLogInfo;
import xyz.zhzby.cyzx.servicebase.domain.PageResponse;
import xyz.zhzby.cyzx.servicebase.domain.RestResponse;

import java.util.List;

/**
 * <p>
 * 评论/回复 服务实现类
 * </p>
 *
 * @author zby
 * @since 2022-02-15
 */
@Slf4j
@Service
public class DiscussionServiceImpl extends ServiceImpl<DiscussionMapper, Discussion> implements IDiscussionService {

    @Override
    public RestResponse<DiscussionResponse> createDiscuss(DiscussionAddRequest discussionAddRequest) {
        User subject = CyzxSecurityContextHolder.getSubject();

        Discussion discussion = DiscussionConvert.INSTANCE.REQUEST_DISCUSSION(discussionAddRequest);
        discussion.setUserId(subject.getId());
        discussion.setUserNickname(subject.getNickName());
        discussion.setUserAvatar(subject.getAvatar());
        subject.fillCreateInfo(discussion, DataLogInfo.USER_FIELD_ACTUAL_NAME);
        baseMapper.insert(discussion);
        DiscussionResponse response = DiscussionConvert.INSTANCE.DISCUSSION_RESPONSE(discussion);
        return RestResponse.success(response);
    }

    @Override
    public RestResponse<PageResponse<DiscussionResponse>> getDiscussionList(DiscussionPageRequest discussionPageRequest) {
        List<DiscussionResponse> res = baseMapper.getDiscussionList(discussionPageRequest);
        LambdaQueryWrapper<Discussion> qw = new LambdaQueryWrapper<>();
        qw.eq(Discussion::getParentId, discussionPageRequest.getOwnerId());
        Integer count = baseMapper.selectCount(qw);
        return RestResponse.success(new PageResponse<>(res, count, discussionPageRequest.getPage(), discussionPageRequest.getLimit()));
    }

    @Override
    public RestResponse<String> reportDiscussion(DiscussionReportRequest discussionReportRequest) {
        log.warn("有ID为{}的评论被举报，原因为：{}", discussionReportRequest.getDiscussionId(), discussionReportRequest.getReportReason());
        //todo 处理举报，可以整个邮件通知管理员，也可以在管理员端加入统一管理
        return RestResponse.success("举报成功，我们将尽快处理，感谢您的反馈");
    }

}
